package com.logistics.team3.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.logistics.team3.common.Page;
import com.logistics.team3.dao.BaseDao;
/**
 * 基础BaseDao的实现类
 * @author limin
 * @version 0.0.1
 * 2015年6月15日上午10:54:23 
 * @param <T>
 */
@Transactional
public class BaseDaoImpl<T> implements BaseDao<T> {
	/**
	 * 获得spring管理的sessionFactory对象
	 */
	private SessionFactory sessionFactory;
	/**
	 * 与泛型中T一样的Class
	 */
	@SuppressWarnings("rawtypes")
	private Class clazz;
	/**
	 * 无参构造方法
	 * 通过反射拿到泛型中T的Class对象
	 */
	public BaseDaoImpl() {
		Type type = this.getClass().getGenericSuperclass();
		clazz = (Class<?>)(((ParameterizedType) type).
				getActualTypeArguments()[0]);
	}
	/**
	 * 添加对象
	 */
	@Override
	public void save(T entity) {
		getSession().save(entity);
	}
	/**
	 * 更新对象
	 */
	@Override
	public void update(T entity) {
		getSession().update(entity);
	}
	/**
	 * 删除对象
	 */
	@Override
	public void delete(T entity) {
		getSession().delete(entity);
	}
	/**
	 * 通过oid查找对象
	 */
	@SuppressWarnings("unchecked")
	@Override
	public T findById(Serializable id) {
		
		return (T) getSession().get(clazz, id);
	}
	/**
	 * 通过 hql 查找无条件的所有该对象(T)的数据
	 * 返回List<T>
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> findAll() {
		StringBuilder builder = new StringBuilder();
		builder.append(" from ").append(clazz.getSimpleName());
		return getSession().createQuery(builder.toString()).list();
	}
	/**
	 * 
	 * @param hql 		 通过hql语句查询对象
	 * @param params  	hql语句中需要的参数
	 * @return			对象集合
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> findByHql(String hql, Object... params) {
		if (hql.indexOf("select") == -1) {
			StringBuilder builder = new StringBuilder();
			builder.append(" select count(*) from ").append(
					clazz.getSimpleName());
			hql = builder.append(" ").append(hql).toString();
		}
		Query query = getSession().createQuery(hql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		return query.list();
	}

	/**
	 * 无条件查询该对象所有数据的记录数
	 * @return 记录数
	 */
	@Override
	public Integer findCount() {
		StringBuilder builder = new StringBuilder();
		builder.append(" select count(*) from ").append(clazz.getSimpleName());
		Long result = (Long) getSession().createQuery(builder.toString())
				.uniqueResult();
		return result.intValue();
	}
	/**
	 * 通过hql语句查询该对象所有数据的记录数
	 * @param hql	  	hql语句
	 * @param params  	hql语句中需要的参数
	 * @return 			记录数
	 */
	@Override
	public Integer findCount(String hql, Object... params) {
		StringBuilder builder = new StringBuilder();
		builder.append(" select count(*) ");
		hql = builder.append(" ").append(hql.substring(hql.indexOf("from")))
				.toString();
		Query query = getSession().createQuery(hql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		return ((Long) query.uniqueResult()).intValue();
	}
	/**
	 * 无条件分页查询
	 * @param pageIndex 当前页码的索引(如第xx页)
	 * @param pageSize 	每页的页码数量 如(每页xx条)
	 * @return 			Page类
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Page<T> findByPage(int pageIndex, int pageSize) {
		StringBuilder builder = new StringBuilder();
		builder.append(" from ").append(clazz.getSimpleName());
		Query query = getSession().createQuery(builder.toString());
		query.setFirstResult((pageIndex - 1) * pageSize);
		query.setMaxResults(pageSize);
		int rowCount = findCount();
		Page<T> page = new Page<T>(pageIndex, pageSize, rowCount);
		page.setLists(query.list());
		return page;
	}
	/**
	 * 通过hql语句 分页查询
	 * @param hql		hql语句
	 * @param pageIndex	当前页码的索引(如第xx页)
	 * @param pageSize	每页的页码数量 如(每页xx条)
	 * @param params 	hql语句中需要的参数
	 * @return 			Page类
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Page<T> findByPage(String hql, int pageIndex, int pageSize,
			Object... params) {
		Query query = getSession().createQuery(hql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		query.setFirstResult((pageIndex - 1) * pageSize);
		query.setMaxResults(pageSize);
		int rowCount = findCount(hql, params);
		Page<T> page = new Page<T>(pageIndex, pageSize, rowCount);
		page.setLists(query.list());
		return page;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
		
	}

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

}
